package com.itheima.service.mongo.impl;

import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.domain.mongo.Video;
import com.itheima.domain.vo.PageBeanVo;
import com.itheima.service.mongo.CommentService;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.List;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    private MongoTemplate mongoTemplate;


    /**
     * comment  已知：
     * publishId：动态id
     * userId：操作人
     * commentType : //操作类型： 1-对动态点赞，2-对动态评论，3-对动态喜欢  4-对视频点赞   5-对视频评论    6-对评论点赞
     *
     * @param comment
     * @return
     */
    @Override
    public Integer saveMovementComment(Comment comment) {
        //1.根据id查询动态详情
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        //2.保存comment
        comment.setCreated(System.currentTimeMillis());
        comment.setPublishUserId(movement.getUserId());//被操作对象所属人
        mongoTemplate.save(comment);
        //3.考虑的是    点赞数 评论数 喜欢数据    +1
        if (comment.getCommentType() == 1) {
            movement.setLikeCount(movement.getLikeCount() + 1);
        } else if (comment.getCommentType() == 2) {
            movement.setCommentCount(movement.getCommentCount() + 1);
        } else if (comment.getCommentType() == 3) {
            movement.setLoveCount(movement.getLoveCount() + 1);
        }
        //更新一下详情表
        mongoTemplate.save(movement);//如果数据库已经存在    执行修改动作  不存在就是新增
        //4.返回次数
        if (comment.getCommentType() == 1) {
            return movement.getLikeCount();
        } else if (comment.getCommentType() == 2) {
            return movement.getCommentCount();
        } else if (comment.getCommentType() == 3) {
            return movement.getLoveCount();
        }
        return 0;
    }

    /**
     * 删除评论列表
     *
     * @param comment
     * @return
     */
    @Override
    public Integer deleteMovementComment(Comment comment) {
        //1.根据动态id查询动态详情
        Movement movement = mongoTemplate.findById(comment.getPublishId(), Movement.class);
        //2.删除comment   动态id    只是定位到某一组操作
        //根据动态id    用户的id   和操作的类型  锁定到某一条数据进行删除
        Query query = new Query(
                new Criteria().where("publishId").is(comment.getPublishId())
                        .and("userId").is(comment.getUserId())
                        .and("commentType").is(comment.getCommentType())
        );

        mongoTemplate.remove(query, Comment.class);

        //3.考虑的是    点赞数 评论数 喜欢数据-1
        if (comment.getCommentType() == 1) {
            movement.setLikeCount(movement.getLikeCount() - 1);
        } else if (comment.getCommentType() == 2) {
            movement.setCommentCount(movement.getCommentCount() - 1);
        } else if (comment.getCommentType() == 3) {
            movement.setLoveCount(movement.getLoveCount() - 1);
        }
        //更新一下详情表
        mongoTemplate.save(movement);// 如果数据库已经存在 执行修改动作 不存在就是新增


        //返回次数
        if (comment.getCommentType() == 1) {
            return movement.getLikeCount();
        } else if (comment.getCommentType() == 2) {
            return movement.getCommentCount();
        } else if (comment.getCommentType() == 3) {
            return movement.getLoveCount();
        }
        return 0;
    }

    /**
     * 分页查询 动态下的评论
     *
     * @param pageNumber
     * @param pageSize
     * @param movementId
     * @return
     */
    @Override
    public PageBeanVo findMovementComment(Integer pageNumber, Integer pageSize, String movementId) {
        int startIndex = (pageNumber - 1) * pageSize;
        //分页查询数据
        Query query = new Query(
                new Criteria().where("publishId").is(new ObjectId(movementId)).and("commentType").is(2)
        )
                .with(Sort.by(Sort.Order.desc("created")))
                .skip(startIndex).limit(pageSize);

        List<Comment> comments = mongoTemplate.find(query, Comment.class);

        //查询总记录数
        long counts = mongoTemplate.count(query, Comment.class);
        return new PageBeanVo(pageNumber, pageSize, counts, comments);

    }

    /**
     * 根据用户的id和操作的类型查询评论列表的数据
     *
     * @param pageNumber
     * @param pageSize
     * @param commentType
     * @param userId
     * @return
     */
    @Override
    public PageBeanVo findCommentByCommentType(Integer pageNumber, Integer pageSize, int commentType, Long userId) {
        int startIndex = (pageNumber - 1) * pageSize;
        Query query = new Query(
                new Criteria().where("publishUserId").is(userId)
                        .and("commentType").is(commentType)
        )
                .with(Sort.by(Sort.Order.desc("created")))
                .skip(startIndex).limit(pageSize);
        //查询记录
        List<Comment> commentList = mongoTemplate.find(query, Comment.class);

        //查询总记录数据
        long counts = mongoTemplate.count(query, Comment.class);

        return new PageBeanVo(pageNumber, pageSize, counts, commentList);
    }

    /**
     * 保存视频评论
     *
     * @param comment
     * @return
     */
    @Override
    public Integer saveVideoComment(Comment comment) {
        //1.根据id查询视频详情
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
        //2.保存comment
        comment.setCreated(System.currentTimeMillis());
        comment.setPublishUserId(video.getUserId());
        mongoTemplate.save(comment);

        //3.        点赞数+1
        if (comment.getCommentType() == 4) {
            video.setLikeCount(video.getLikeCount() + 1);
        } else if (comment.getCommentType() == 5) {
            video.setCommentCount(video.getCommentCount() + 1);
        }
        //更新一下详情表
        mongoTemplate.save(video);//如果数据库已经存在   执行修改操作  不存在就是新增

        return null;
    }

    /**
     * 删除视频评论
     *
     * @param comment
     * @return
     */
    @Override
    public Integer deleteVideoLike(Comment comment) {
        //1.根据id查询视屏详情
        Video video = mongoTemplate.findById(comment.getPublishId(), Video.class);
        //2.保存comment
        comment.setPublishUserId(video.getUserId());
        Query query = new Query(
                Criteria.where("publishId").is(comment.getPublishId())
                        .and("userId").is(comment.getUserId())
                        .and("commentType").is(comment.getCommentType())
        );
        mongoTemplate.remove(query, Comment.class);
        //3.点赞数 -1
        if (comment.getCommentType() == 4) {
            video.setLikeCount(video.getLikeCount() - 1);
        }
        mongoTemplate.save(video);//更新视频详情  存在即修改   不存在 即添加
        return null;
    }

    /**
     * 视频评论点赞
     * @param comment
     * @return
     */
    @Override
    public Integer videoCommentLike(Comment comment) {
        //1.根据id查询动态详情
        Comment comment1 = mongoTemplate.findById(comment.getPublishId(), Comment.class);
        //2.保存comment
        comment.setCreated(System.currentTimeMillis());
        comment.setPublishUserId(comment1.getUserId());//被操作对象所属人
        mongoTemplate.save(comment);
        //3.考虑的是    点赞数 评论数 喜欢数据    +1
        if (comment.getCommentType() == 6) {
            comment1.setLikeCount(comment1.getLikeCount() + 1);
        }
        //更新一下详情表
        mongoTemplate.save(comment1);//如果数据库已经存在    执行修改动作  不存在就是新增
        if (comment.getCommentType() == 6) {
            return comment1.getLikeCount();
        }
        return 0;
    }

    /**
     * 取消视频评论点赞
     * @param comment
     * @return
     */
    @Override
    public Integer deleteVideoCommentLike(Comment comment) {
        //1.根据id查询动态详情
        Comment comment1 = mongoTemplate.findById(comment.getPublishId(), Comment.class);
        //2.删除comment   动态id    只是定位到某一组操作
        //根据动态id    用户的id   和操作的类型  锁定到某一条数据进行删除
        Query query = new Query(
                new Criteria().where("publishId").is(comment.getPublishId())
                        .and("userId").is(comment.getUserId())
                        .and("commentType").is(comment.getCommentType())
        );

        mongoTemplate.remove(query, Comment.class);
        //3.考虑的是    点赞数 评论数 喜欢数据    +1
        if (comment.getCommentType() == 6) {
            comment1.setLikeCount(comment1.getLikeCount() - 1);
        }
        //更新一下详情表
        mongoTemplate.save(comment1);// 如果数据库已经存在 执行修改动作 不存在就是新增
        if (comment.getCommentType() == 6) {
            return comment1.getLikeCount();
        }
        return 0;
    }

    /**
     * 动态评论点赞
     * @param comment
     * @return
     */
    @Override
    public Integer movementCommentLike(Comment comment) {
        //1.根据id查询动态详情
        Comment comment1 = mongoTemplate.findById(comment.getPublishId(), Comment.class);
        //2.保存comment
        comment.setCreated(System.currentTimeMillis());
        comment.setPublishUserId(comment1.getUserId());//被操作对象所属人
        mongoTemplate.save(comment);
        //3.考虑的是    点赞数 评论数 喜欢数据    +1
        if (comment.getCommentType() == 6) {
            comment1.setLikeCount(comment1.getLikeCount() + 1);
        }
        //更新一下详情表
        mongoTemplate.save(comment1);//如果数据库已经存在    执行修改动作  不存在就是新增
        if (comment.getCommentType() == 6) {
            return comment1.getLikeCount();
        }
        return 0;
    }

    /**
     * 取消动态评论点赞
     * @param comment
     * @return
     */
    @Override
    public Integer deleteMovementCommentLike(Comment comment) {
        //1.根据id查询动态详情
        Comment comment1 = mongoTemplate.findById(comment.getPublishId(), Comment.class);
        //2.删除comment   动态id    只是定位到某一组操作
        //根据动态id    用户的id   和操作的类型  锁定到某一条数据进行删除
        Query query = new Query(
                new Criteria().where("publishId").is(comment.getPublishId())
                        .and("userId").is(comment.getUserId())
                        .and("commentType").is(comment.getCommentType())
        );

        mongoTemplate.remove(query, Comment.class);
        //3.考虑的是    点赞数 评论数 喜欢数据    +1
        if (comment.getCommentType() == 6) {
            comment1.setLikeCount(comment1.getLikeCount() - 1);
        }
        //更新一下详情表
        mongoTemplate.save(comment1);// 如果数据库已经存在 执行修改动作 不存在就是新增
        if (comment.getCommentType() == 6) {
            return comment1.getLikeCount();
        }
        return 0;
    }
}